<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="input3" class="input-with-select" suffix-icon="el-icon-search" />
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addDialogFormVisible = true">添加商品</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="id" label="ID" width="180">
            </el-table-column>
            <el-table-column prop="name" label="商品名称" width="180">
            </el-table-column>
            <el-table-column prop="price" label="商品价格">
            </el-table-column>
            <el-table-column prop="datetime" label="上架时间">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" icon="el-icon-edit" circle></el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete" circle @click="del(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="query.currentPage" :page-sizes="[5, 10, 20]" :page-size="query.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="query.total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 添加商品 -->
    <el-dialog title="添加商品" :visible.sync="addDialogFormVisible">
      <el-form>
        <el-form-item label="商品名称" label-width="80px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" label-width="80px">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上架时间" label-width="80px">
          <el-date-picker v-model="form.datetime" type="datetime" placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSku">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  import Mock from 'mockjs'
  export default {
    data() {
      return {
        tableData: [],
        query: {
          currentPage: 1,
          pageSize: 5,
          total: 0
        },
        //添加商品弹框
        addDialogFormVisible: false,
        // 商品from
        form: {
          id: 0,
          name: '',
          price: 0,
          datetime: ''
        }
      }
    },
    mounted() {
      this.getSkuList(1, 5);
    },
    methods: {
      getSkuList(currentPage, pageSize) {
        //json格式字符串
        let skuList = localStorage.getItem("skulist");
        skuList = JSON.parse(skuList);
        //总条数
        this.query.total = skuList.length;
        //开始下标
        let startIndex = (currentPage - 1) * pageSize;
        //结束下标
        let endIndex = currentPage * pageSize;
        //分页数据
        skuList = skuList.slice(startIndex, endIndex);
        this.tableData = skuList;
      },
      //切换每页多少条
      handleSizeChange(val) {
        this.query.pageSize = val;
        this.getSkuList(this.query.currentPage, val);
      },
      //切换上下页
      handleCurrentChange(val) {
        this.query.currentPage = val;
        this.getSkuList(val, this.query.pageSize);
      },
      //添加商品
      addSku() {
        //json格式的字符串
        let skuList = localStorage.getItem("skulist");
        //转成数组对象
        skuList = JSON.parse(skuList);
        //生成数据模板
        let tpl = {
          'sku|1': [{
            // 'id':'@id()',
            'id|+1': 1,
            // 'name':'@title(2)',
            'name': '@cname',
            'price': '@float(1000, 10000,2,2)',
            "salesVolume|30-1000": 30,
            'datetime': '@datetime()',
            'region': '@region()',
            'province': '@province()',
            "star|1-5": "★",
            "address": "@county(true)", //随机生成地址
            "web": "@web()",
          }]
        }
        //生成的数据对象
        const data = Mock.mock(tpl);
        //把用户输入的数据赋值给生成的数据对象中
        data.sku.id = Math.ceil(Math.random() * 10000);
        data.sku.name = this.form.name;
        data.sku.price = this.form.price;
        data.sku.datetime = this.form.datetime;
        //添加到数组的开头
        skuList.unshift(data.sku);
        //存储
        localStorage.setItem("skulist", JSON.stringify(skuList));
        //获取最新数据
        this.getSkuList(this.query.currentPage, this.query.pageSize);
        //关闭弹框
        this.addDialogFormVisible = false;
        this.$message({
          message: '添加商品成功!',
          type: 'success'
        });
      },
      //删除商品
      del(row) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //json格式字符串
          let skuList = localStorage.getItem("skulist");
          //数组对象
          skuList = JSON.parse(skuList);
          skuList = skuList.filter(function(val) {
            return val.id != row.id;
          });
          //存
          localStorage.setItem("skulist", JSON.stringify(skuList));
          //获取最新数据
          this.getSkuList(this.query.currentPage, this.query.pageSize);
          this.$message({
            message: '删除商品成功!',
            type: 'success'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style scoped>
  .el-pagination {
    margin-top: 10px;
  }

  .el-breadcrumb {
    margin-bottom: 15px;
  }
</style>
